import os
import time

from PyQt5.QtGui import QPalette, QFont, QPixmap
from PyQt5.QtCore import Qt, QTimer, QDateTime
from PyQt5.QtWidgets import *
import sys
import lib.db as db
from Form1 import Form1
from Form2 import Form2
from Form3 import Form3
from Form4 import Form4

import threading


class TextEditDemo(QWidget):
    def __init__(self, parent=None):
        super(TextEditDemo, self).__init__(parent)
        self.setWindowTitle("QStackedLayout 例子")
        self.resize(1598, 842)

        # 设置lcd时钟
        self.Timer = QTimer()  # 自定义QTimer
        self.Timer.start(500)  # 每0.5秒运行一次
        self.Timer.timeout.connect(self.updateTime)  # 连接updateTime

        # 创建堆叠按钮
        self.btnPress1 = QPushButton("FormA")
        self.btnPress1.setFixedSize(60, 60)
        self.btnPress2 = QPushButton("FormB")
        self.btnPress2.setFixedSize(60, 60)
        self.btnPress3 = QPushButton("FormC")
        self.btnPress3.setFixedSize(60, 60)
        self.btnPress4 = QPushButton("FormD")
        self.btnPress4.setFixedSize(60, 60)

        # 添加页面
        self.form1 = Form1()
        self.form2 = Form2()
        self.form3 = Form3()
        self.form4 = Form4()

        # <editor-fold desc="设置LOGO">
        self.head_img_lable = QLabel(self)
        self.head_img_lable.setMaximumSize(40, 40)  # 调整logo大小
        head_pic = QPixmap('./pic/WOW.png')
        self.head_img_lable.setPixmap(head_pic)
        self.head_img_lable.setScaledContents(True)  # 图片自适应LABEL大小
        # </editor-fold>

        # <editor-fold desc="设置标题">
        self.head_name_label = QLabel(self)
        self.head_name_label.setText('公交优先控制系统')
        pe = QPalette()
        pe.setColor(QPalette.WindowText, Qt.red)  # 设置字体颜色
        self.head_name_label.setAutoFillBackground(True)  # 设置背景充满，为设置背景颜色的必要条件
        pe.setColor(QPalette.Window, Qt.white)  # 设置背景颜色
        self.head_name_label.setPalette(pe)
        self.head_name_label.setFont(QFont("Roman times", 25, QFont.Bold))
        self.head_name_label.resize(40, 40)
        # </editor-fold>

        # <editor-fold desc="设置LCD">
        self.lcd_time = QLCDNumber(self)
        self.lcd_time.setDigitCount(10)  # 初始显示
        self.lcd_time.setDigitCount(20)  # 位数限制
        self.lcd_time.setSegmentStyle(QLCDNumber.Flat)  # 内部填充
        self.lcd_time.setStyleSheet("border: 1px solid green; color: green; background: silver;")  # 样式表单
        # </editor-fold>

        # <editor-fold desc="添加堆叠窗口">
        widget = QWidget()
        self.stacked_layout = QStackedLayout()
        widget.setLayout(self.stacked_layout)
        widget.setStyleSheet("background-color:white;")
        self.stacked_layout.addWidget(self.form1)
        self.stacked_layout.addWidget(self.form2)
        self.stacked_layout.addWidget(self.form3)
        self.stacked_layout.addWidget(self.form4)
        # </editor-fold>

        # <editor-fold desc="添加head">
        head_layout = QHBoxLayout()  # 添加水平布局
        head_layout.addWidget(self.head_img_lable)
        head_layout.addWidget(self.head_name_label)
        head_layout.addWidget(self.btnPress1)
        head_layout.addWidget(self.btnPress2)
        head_layout.addWidget(self.btnPress3)
        head_layout.addWidget(self.btnPress4)
        head_layout.addStretch(7)  # 添加弹簧
        head_layout.addWidget(self.lcd_time)
        layout = QVBoxLayout()  # 添加垂直布局
        layout.addItem(head_layout)
        layout.addWidget(widget)
        # </editor-fold>

        self.setLayout(layout)
        self.btnPress1.clicked.connect(self.btnPress1_Clicked)
        self.btnPress2.clicked.connect(self.btnPress2_Clicked)
        self.btnPress3.clicked.connect(self.btnPress3_Clicked)
        self.btnPress4.clicked.connect(self.btnPress4_Clicked)

    def updateTime(self):
        self.lcd_time.display(QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss'))  # 显示时间的格式

    def btnPress1_Clicked(self):
        self.stacked_layout.setCurrentIndex(0)

    def btnPress2_Clicked(self):
        self.stacked_layout.setCurrentIndex(1)

    def btnPress3_Clicked(self):
        self.stacked_layout.setCurrentIndex(2)

    def btnPress4_Clicked(self):
        self.stacked_layout.setCurrentIndex(3)


def Run_UI():
    db.Init_DB()
    app = QApplication(sys.argv)
    win = TextEditDemo()
    win.show()
    sys.exit(app.exec_())


def Live_Server():
    cmd_code = 'python -m http.server 8080 --bind 127.0.0.1'
    result = os.system(cmd_code)


if __name__ == "__main__":
    threading.Thread(target=Run_UI).start()  # 启动UI界面线程
    # threading.Thread(target=Live_Server).start()  # 启动Web服务器线程
